﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChurchData.Business
{
    public class LKRelationshipBO : ChurchDataBusinessBase
    {
        public LKRelationship GetRecordByPK(int pk)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            return (from rec in oDB.LKRelationships
                    where rec.LKRelationshipPK == pk
                    select rec).First();
        }

        public List<GenericLKTableResults> GetActiveRecords()
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            var x =
                    from oLK in oDB.LKRelationships
                    where oLK.ActiveInd == true
                    orderby oLK.DescriptionOne ascending
                    select oLK;


            List<GenericLKTableResults> oList = new List<GenericLKTableResults>();
            foreach (LKRelationship oLK in x)
            {
                GenericLKTableResults oGLK = new GenericLKTableResults();
                oGLK.Pk = oLK.LKRelationshipPK;
                oGLK.Description = oLK.DescriptionOne;
                oGLK.DescriptionTwo = oLK.DescriptionTwo;
                oList.Add(oGLK);
            }
            return oList;
        }

        public int InsertRecord(string descriptionOne, string descriptionTwo)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                LKRelationship oLK = new LKRelationship();
                oLK.ActiveInd = true;
                oLK.DescriptionOne = descriptionOne;
                oLK.DescriptionTwo = descriptionTwo;
                oLK.LastUpdUser = UserInformation.UserName;
                oLK.LastUpdTimestamp = DateTime.Now;
                oDB.LKRelationships.InsertOnSubmit(oLK);
                oDB.SubmitChanges();
                return oLK.LKRelationshipPK;
            }
        }

        public bool ModifyRecord(int pk, string descriptionOne, string descriptionTwo)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                var x = (from oLK in oDB.LKRelationships
                         where oLK.LKRelationshipPK == pk
                         select oLK).Single();
                x.DescriptionOne = descriptionOne;
                x.DescriptionTwo = descriptionTwo;
                x.LastUpdUser = UserInformation.UserName;
                x.LastUpdTimestamp = DateTime.Now;
                oDB.SubmitChanges();
                return true;
            }
        }

        public bool SetToInactive(int pk)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                var x = (from oLK in oDB.LKRelationships
                         where oLK.LKRelationshipPK == pk
                         select oLK).Single();
                x.ActiveInd = false;
                x.LastUpdUser = UserInformation.UserName;
                oDB.SubmitChanges();
            }

            return true;
        }

        public List<SimpleRelationship> GetRelationshipsForwardAndBackward()
        {
            List<SimpleRelationship> result = new List<SimpleRelationship>();

            List<GenericLKTableResults> relationships = this.GetActiveRecords();

            // First add them all
            foreach (GenericLKTableResults relationship in relationships)
            {
                SimpleRelationship sr = new SimpleRelationship();
                sr.LKRelationshipPK = relationship.Pk;
                sr.DescriptionOne = relationship.Description;
                sr.DescriptionTwo = relationship.DescriptionTwo;
                sr.Flipped = false;
                result.Add(sr);
            }

            // Now add them again switching description1 and description2
            foreach (GenericLKTableResults relationship in relationships)
            {
                if (relationship.Description.Trim() == relationship.DescriptionTwo.Trim()) continue;
                SimpleRelationship sr = new SimpleRelationship();
                sr.LKRelationshipPK = relationship.Pk;
                sr.DescriptionOne = relationship.DescriptionTwo;
                sr.DescriptionTwo = relationship.Description;
                sr.Flipped = true;
                result.Add(sr);
            }

            return result;
        }
    }//class
}//namespace
